//
//  AVRequest+SignIn.m
//  项目三
//
//  Created by rimi1 on 2016/12/9.
//  Copyright © 2016年 Wangxingqian. All rights reserved.
//

#import "AVRequest+SignIn.h"

@implementation AVRequest (SignIn)

// 登录
+ (void)avSignInWithAccount:(NSString *)account password:(NSString *)password signIn:(QueryArr)signIn {
    // 动画
    UIApplication *app = [UIApplication sharedApplication];
    [app showCircleAnimationWithTitle:@"加载中"];
    
    AVQuery *query = [AVQuery queryWithClassName:@"MyUser"];
    [query whereKey:@"account" equalTo:account];
    [AVRequest avQueryWithQuery:query queryArr:^(NSArray *objects, NSError *error) {
        [app hideCircleAnimation];
        if (signIn) {
            signIn(objects, error);
        }
    }];
}

@end

@implementation AVRequest (Enroll)

+ (void)avAccountExistWithAccount:(NSString *)account succeed:(AVSucceed)succeed {
    AVQuery *query = [AVQuery queryWithClassName:@"MyUser"];
    [query whereKey:@"account" equalTo:account];
    [AVRequest avQueryWithQuery:query queryArr:^(NSArray *objects, NSError *error) {
        if (error) {
            succeed(NO, error);
            return;
        }
        
        if (objects.count == 0) {
            succeed(NO, error);
        }else {
            succeed(YES, error);
        }
    }];
}

+ (void)avNameExistWithName:(NSString *)name succeed:(AVSucceed)succeed {
    AVQuery *query = [AVQuery queryWithClassName:@"MyUser"];
    [query whereKey:@"name" equalTo:name];
    [AVRequest avQueryWithQuery:query queryArr:^(NSArray *objects, NSError *error) {
        if (error) {
            succeed(NO, error);
            return;
        }
        
        if (objects.count == 0) {
            succeed(NO, error);
        }else {
            succeed(YES, error);
        }
    }];
}

// 注册(因为在输入账号这些，已经判断过，这里不需再次判断账号存在了)
+ (void)avEnrollWithAccount:(NSString *)account password:(NSString *)password name:(NSString *)name sex:(NSString *)sex avSucceed:(AVSucceed)avSucceed {
    // 动画
    UIApplication *app = [UIApplication sharedApplication];
    [app showCircleAnimationWithTitle:@"加载中"];
    
    AVObject *user = [AVObject objectWithClassName:@"MyUser"];
    [user setObject:account forKey:@"account"];
    [user setObject:password forKey:@"password"];
    [user setObject:name forKey:@"name"];
    [user setObject:sex forKey:@"sex"];
    // 默认头像
    [user setObject:@"http://ac-NF0RyIDf.clouddn.com/7uWjodKPfU07SKvf75r3BCD.png" forKey:@"headImage"];
    
    [user saveInBackgroundWithBlock:^(BOOL succeeded, NSError * _Nullable error) {
        [app hideCircleAnimation];
        if (avSucceed) {
            avSucceed(succeeded, error);
        }
    }];
}



@end













